package com.duke.shaking.widget.gallery;

import java.util.List;

import com.androidquery.AQuery;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridImageAdapter extends BaseAdapter {

	private Context context;
	private List<String> imagesUrl;
	private AQuery aQuery;
	private int imageWidth;
	private int imageHeight;
	
	public GridImageAdapter(Context context, List<String> imagesUrl) {
		this.context = context;
		this.imagesUrl = imagesUrl;
		aQuery = new AQuery(context);
	}

	public void setImagesUrl(List<String> imagesUrl) {
		this.imagesUrl = imagesUrl;
	}
	
	public int getCount() {
		if (imagesUrl == null) {
			return 0;
		}
		return imagesUrl.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public void setImageSize(int width, int height) {
		imageWidth = width;
		imageHeight = height;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		
		BorderImageView imageView;

		if (convertView == null) {
			imageView = new BorderImageView(context); 
			imageView.setLayoutParams(new GridView.LayoutParams(
						imageWidth, imageHeight));
			/*
			if (imageCol == 4) {
				imageView.setLayoutParams(new GridView.LayoutParams(
						dm.heightPixels / imageCol - 6, dm.heightPixels
								/ imageCol - 6));
			} else {
				imageView.setLayoutParams(new GridView.LayoutParams(
						dm.widthPixels / imageCol - 6, dm.widthPixels
								/ imageCol - 6));
			}*/
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

			// imageView.setPadding(3, 3, 3, 3);
		} else {
			imageView = (BorderImageView) convertView;
		}
		
		//imageView.setImageResource(ImageSource.mThumbIds[position]);

		if (imagesUrl != null && position < imagesUrl.size()) {
			aQuery.id(imageView).image(imagesUrl.get(position), false, true);
		}
		
		return imageView;
	}

	


}
